import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

class Zhangsan{
    public void findLove(){
        System.out.println("符合要求");
    }

    public void say(){
        System.out.println("aaaaaaaaaaa");
    }
}

public class CGLibMeipo implements MethodInterceptor {
    public Object getInstance(Class<?> clazz) throws Exception{
        //相当于JDK中的Proxy类，完成代理的工具类，通过继承实现
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return enhancer.create();
    }

    public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        before();;
        Object res = methodProxy.invokeSuper(proxy, args);
        after();
        return res;
    }

    private void before(){
        System.out.println("开始物色");
    }

    private void after(){
        System.out.println("开始交往");
    }

    public static void main(String[] args) {
        try{
            Zhangsan obj = (Zhangsan)new CGLibMeipo().getInstance(Zhangsan.class);
            obj.findLove();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
